home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ADA Programming Guide
/
ADA Programming Guide.iso
/
ada_pcdp
/
adas
/
adas.doc
next >
Wrap
Text File
|
1996-01-30
|
9KB
|
204 lines
AdaS Concurrency Simulator
for
Principles of Concurrent and Distributed Programming
M. Ben-Ari
Prentice-Hall International
1989
1. Introduction
The AdaS program simulates concurrent execution of several processes
written in Ada syntax. There are two principle reasons for using AdaS,
even if a true Ada compiler is available:
a. Some Ada compilers may not implement time slicing. Therefore,
they cannot execute the common memory algorithms.
b. Even if time-slicing is implemented, the user may not be able to
control the scheduler in sufficient detail to demonstrate the pathological
behavior described in the book.
For example, AdaS can demonstrate livelock in the fourth example of
Chapter 3 which is caused by perfect interleaving of the instructions
of two processes.
AdaS is NOT an Ada interpreter. It accepts only very small fragment of
the language, it does not reject all incorrect programs and there are
some deviations from Ada semantics. It is best used as a laboratory
instrument where an example program is initially written using a true
compiler and then transfered to AdaS for microscopic examination.
2. Implementation
AdaS is a descendent of the Pascal-S interpreter written by N. Wirth of
ETH (Zurich, Switzerland) in 1976. Pascal-S is described in [1].
The interpreter was modified to support concurrency by the author [2].
AdaS is a modification of this concurrent interpreter to accept Ada
syntax and the Ada rendezvous. Finally, the user interface has been
improved to support microscopic control of the concurrency scheduler.
The program was written using Turbo Pascal (version 5.0) from Borland
International on PC-compatible personal computers. The deviations
from standard Pascal are relatively minor, except for the use of Units
to break up the program into modules. Since units are common in other
dialects of Pascal, it should be possible to port the program.
3. Specification
The following fragment of the Ada language is supported:
3.1 Constants, variables and expressions of type integer, character
and boolean. Arithmetical and logical operators on integers and booleans.
Initial values for static variables. Assignment statements. One
dimensional arrays of the predefined types. No array assignments.
3.2 Control statements: loop (infinite or with exit), for, while,
if, null.
3.3 Procedures (no functions) with parameters. "in" mode uses copy
semantics and "out" or "in out" use reference semantics like Pascal.
No default parameters.
3.4 Text_IO procedures: get, put, skip_line, new_line, put_line.
3.5 Tasks objects (up to 3) declared in the main procedure. Entry calls
and accept statements which may have up to two parameters of mode
"in" or "out" and predefined type. The task specification is ignored
and the first accept statement is the defining occurence of the entry.
Hence the entry calls can only be used in tasks declared after the
accepting task.
3.6 One task may have a two branch select statement with guards and a
terminate alternative. This is designed to allow a bounded buffer
to be programmed.
3.7 Semaphores (as if they were declared in a package).
3.8 Context clauses and pragmas are ignored. This facilitates transfering
a program from a true compiler to AdaS.
4. Usage
Upon executing AdaS, it prompts for the name of a source file.
A non-alphabetic first character will terminate AdaS.
The extension ".ADA" is added to the name entered. The next prompt
asked if a listing is requested. If so, it will be created on a
file with the same name and with the extension ".LIS".
The listing file will contain the source statements as well
as the "machine code", i.e. the byte codes generated for use
by the interpreter.
If compilation is unsuccessful, the incorrect line will be
displayed. As in Turbo Pascal, no attempt is made to continue
compilation after an error. Press return to terminate the AdaS.
If compilation is successful, AdaS prompts to ask if the program
should be interpreted. If so, the next prompt asks for process
priorities. A zero answer gives all processes the same priority.
Otherwise, enter three priorities from the range 1 to 3 for the
three processes in the program.
The screen is now divided into three windows. The upper half
of the screen is the program window which displays the results
of print statements from within the program. The lower right
window is the watch window which displays the values of global
variables as they change (note: the initial value is not displayed).
The lower left window prints the status of the processes and
accepts commands from the user. For each process the following
information is displayed: its index, suspension status (0 = active,
999 = inactive, other positive integer = entry or semaphore
upon which it is suspended), current program counter and the
instruction at that location. For each entry, one of two lines of
information is displayed. The process index and pc of a task
suspended at an accept statement for the entry, or the callers
(process index and time-stamp) that are engaged in a rendezvous
or waiting in the entry queue.
The following commands are accepted:
* - execute one instruction of the current process
+ - execute one instruction of the next process
- - execute until termination
/ - terminate program
(space) - the program prompts for a (positive) number of steps to
execute before breaking again
At any time, pressing a key such as space will break the execution
of the program and prompt for a new command.
5. Examples
The directory contains several Ada programs that have been
executed using AdaS:
incr - simultaneous increment of a global integer
first - first attempt at mutual exclusion
second - second attempt at mutual exclusion
third - third attempt at mutual exclusion
fourth - fourth attempt at mutual exclusion
dekker - Dekker's algorithm
peter - Peterson's algorithm
mesem - mutual exclusion using semaphores
pca - producer-consumer using bounded buffer in Ada
If the "+" key is used to acheive perfect interleaving, then
the third attempt will deadlock and the fourth attempt will livelock.
Try changing priorities to vary the behavior of the producer-consumer.
In addition, a program "ren.ada" has two tasks calling the same
entry in a third task. Giving the calling tasks higher priority than
the accepting task can be used to demonstrate the FIFO entry queues.
Note that this program will deadlock; use '/' to terminate execution.
6. Documentation
The AdaS program consists of 7 files:
adas - main program
global - global constants, types and variables
compile - main program of the compiler
util - utility programs used by the compiler
expr - compilation of expressions
state - compilation of statements
interp - the concurrent interpreter
The compiler is a simple recursive descent compiler like the one
described in [3]. It has been simplified further by removing
recovery and terminating upon discovery of an error.
The interpreter consists of a loop that executes the byte
codes that were emitted by the compiler. The code is for
a stack based machine with no registers and is quite straightforward
except for procedure calls and tasking. These are explained in [2].
The four units of the compiler are relatively independent of the
interpreter so that it should be possible to modify the interpreter
without a detailed understanding of the compiler.
Simulation of entry call and accept is done by keeping a table of
entries which records the program counter of a waiting accept
statement and the process index of a waiting call. Calls are
time-stamped to maintain the FIFO queue. Passing parameters
from one task to another is difficult and is done by having
the calling process store the parameters (values for "in" mode
and addresses for "out" mode) in its process table where they can
be picked up by the accepting task.
Select is simulated by checking each guard and each entry
queue sequentially. Random selection of an alternative is done
by skipping the first alternative conditionally upon the value
of a random number. An extra loop around the select processing
ensures that the first alternative is still taken if the
second is closed. If the task with the select notes that it is
the only task left it terminates, thus simulating the terminate alternative.
The Ada tasking support of the AdaS program is heavily commented to
assist readers who wish to modify the program.
7. References
[1] D.W. Barron. Pascal-the Language and its Implementation.
John Wiley. 1981.
[2] M. Ben-Ari. Principles of Concurrent Programming.
Prentice-Hall International. 1982.
[3] J. Welsh and M. McKeag. Structured System Programming.
Prentice-Hall International. 1980.